package net.anzhuo.controller.db.syncdata;



import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.utils.RandomGUID;
import net.anzhuo.entity.GsPermission;
import net.anzhuo.entity.GsPunishment;
import net.anzhuo.entity.db.syncdata.Penalty;
import net.anzhuo.entity.db.syncdata.Permission;
import net.anzhuo.entity.db.syncdata.QueryPermissionResult;
import net.anzhuo.entity.db.syncdata.QueryPunishmentResult;
import net.anzhuo.entity.db.syncdata.Result;
import net.anzhuo.entity.db.syncdata.PageUtilsPermission;
import net.anzhuo.entity.db.syncdata.Paging;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsPermissionService;
import net.anzhuo.service.GsPunishmentService;

@RestController
@Slf4j
public class PermissionController {

	@Autowired
	private GsPermissionService gsPermissionService;


	
	
	
	@RequestMapping("/querypermission")
	public QueryPermissionResult querypermission(String keyNo,String date,String pageIndex, Integer pageSize) {
		log.info("===>querypermission="+keyNo);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
		if (StringUtils.isBlank(pageIndex) || "0".equals(pageIndex)) {
			pageIndex = "1";
		}
		if (pageSize==null || pageSize<1 || pageSize>30) {
			pageSize = 10;
		}
		QueryPermissionResult detailResult = new QueryPermissionResult();
		try {
	
		detailResult.setStatus("200");
		detailResult.setMessage("查询成功");
		GsPermission  p=new GsPermission();
		p.setCoMd5(keyNo);
		p.setUpdateTime(date);
		List<GsPermission> listp=gsPermissionService.getByUpdateTime(p);
		
		Paging paging = new Paging();
		paging.setPageIndex(Integer.parseInt(pageIndex));
		paging.setPageSize(pageSize);
		
		paging.setTotalRecords(listp.size());
		detailResult.setPaging(paging);
		List<Permission> listPermission=new ArrayList<Permission>();
		if(listp!=null)
		{
			Permission pl=new Permission();
			for(GsPermission g:listp)
			{
				pl=new Permission();
				pl.setContent(g.getContent());
				pl.setLegal(g.getLegal());
				pl.setDecisionDate(g.getDecisionDate());
				pl.setName(g.getName());
				pl.setPermissionAgent(g.getPermissionAgent());
				pl.setPermissionNo(g.getPermissionNo());
				pl.setPermissionType(g.getType());
				pl.setPostNo(g.getPostNo());
				pl.setUpdateTime(g.getUpdateTime());
				pl.setStopDate(g.getStopDate());
				listPermission.add(pl);
			}
		}
		PageUtilsPermission pup=new PageUtilsPermission(listPermission,paging.getPageIndex().intValue(),paging.getPageSize().intValue());
		
		detailResult.setPermission(pup.result);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" querypermission 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
	@RequestMapping("/updatepermission")
	public Result updatepermission(String permissionNo,String type,String content,String decisionDate,String stopDate,String postNo,String permissionAgent,String updateTime,String name,String keyNo,String legal) {
		log.info("===>updatepermission="+keyNo);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		Result detailResult = new Result();
		try {
	
		detailResult.setStatus("200");
		detailResult.setMessage("查询成功");
		GsPermission  p=new GsPermission();
		p.setCoMd5(keyNo);
		p.setContent(content);
		p.setType(type);
		p.setDecisionDate(decisionDate);
		p.setLegal(legal);
		p.setName(name);
		p.setPermissionAgent(permissionAgent);
		p.setPermissionNo(permissionNo);
		p.setPostNo(postNo);
		p.setStopDate(stopDate);
		p.setUpdateTime(updateTime);
		p.setMd5(RandomGUID.generatorGUID());
		List<GsPermission> listp=gsPermissionService.getBycoMd5(keyNo);
		boolean b=false;
		if(listp!=null)
		{
			for(GsPermission g:listp)
			{
				if(g.getName().equals(p.getName())&&g.getContent().equals(p.getContent())&&g.getDecisionDate().equals(p.getDecisionDate()))
				{
					b=true;
					detailResult.setStatus("201");
					detailResult.setMessage("数据已存在");
					break;
				}
			}
		}
		if(!b)
		{
			gsPermissionService.save(p);
		}		
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" updatepermission 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
}
